package com.aramco.fion.model.presentation
{
	import com.aramco.fion.event.LoadQueryEvent;
	import com.aramco.fion.util.BSPReportDataModelOO;
	import com.aramco.fion.view.componentsManagement.ChartWizard;
	import com.aramco.fion.view.componentsManagement.PreviewChart;
	import com.aramco.fion.view.componentsManagement.SeriesAndAxis;
	import com.aramco.fion.vo.Callbacks;
	import com.aramco.fion.vo.DashboardVo;
	import com.aramco.fion.vo.FionVo;
	import com.aramco.fion.vo.GroupViewVO;
	import com.aramco.fion.vo.SummaryViewVO;
	
	import flash.display.DisplayObject;
	import flash.events.IEventDispatcher;
	
	import mx.charts.AreaChart;
	import mx.charts.BarChart;
	import mx.charts.CategoryAxis;
	import mx.charts.ColumnChart;
	import mx.charts.LineChart;
	import mx.charts.PieChart;
	import mx.charts.chartClasses.CartesianChart;
	import mx.charts.chartClasses.ChartBase;
	import mx.charts.chartClasses.Series;
	import mx.charts.series.AreaSeries;
	import mx.charts.series.BarSeries;
	import mx.charts.series.ColumnSeries;
	import mx.charts.series.LineSeries;
	import mx.charts.series.PieSeries;
	import mx.collections.ArrayCollection;
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.events.IndexChangedEvent;
	import mx.rpc.events.FaultEvent;
	
	import spark.components.Panel;
	
	public class ChartWizardPresentationModel
	{
		public function ChartWizardPresentationModel()
		{
		}
		
		[Dispatcher]
		public var dispatcher:IEventDispatcher;
		
		public var queryName:String;
		public var chartType:String;
		public var series:ArrayCollection;
		public var axis:Object;
		public var view:ChartWizard;
		public var type:String;
		public var showDataTips:Boolean = true;
		
		//Line Chart Only
		public var interpolateValues:Boolean= true;
		public var form:String;
		
		//Pie Chart Only
		public var labelPlacement:String;
		
		public var queryOutputMeta:ArrayCollection ;
		public var queryOutput:ArrayCollection ;
		
		[ViewAdded]
		public function viewAdded( view : ChartWizard ) : void
		{
			this.view = view;
		}
			
		public function loadQueryMeta(queryName:String):void
		{
			if(queryName != null && queryName!="")
			{
				var handlers : Callbacks = new Callbacks(onResult_loadData);
				var myEvent:LoadQueryEvent = new LoadQueryEvent(LoadQueryEvent.LOAD_XML_REPORT_EVENT,handlers);
				myEvent.queryName = queryName;
				dispatcher.dispatchEvent(myEvent);
			}
		}
		
		private function onResult_loadData(data:BSPReportDataModelOO):void
		{
			view.myViewStack.selectedIndex = 1;
			queryOutputMeta = data.metaData;
			queryOutput = data.repotData;
		}
		
		
		public function selectChartType(chartType:String, view:SeriesAndAxis):void
		{
			this.chartType = chartType;
			view.currentState = chartType;
			/*if(chartType == "PieChart")
				view.currentState = "PieChart";
			else
				view.currentState = "";*/
		}
		
		public function next(obj:DisplayObject):void
		{
			if(obj is SeriesAndAxis)
			{
				series = (obj as SeriesAndAxis).dg1.getSelectedRecords();
				axis =  (obj as SeriesAndAxis).dg2.getSelectedRecords();
				chartType = (obj as SeriesAndAxis).chartTypeDropDownList.selectedItem.value;
				view.myViewStack.selectedIndex = 2;
			}
			
			else if(obj is PreviewChart)
			{
				
			}
		}
		
		public function prev():void
		{
			
		}
		
		public function refreshChart(outputView:spark.components.Panel):void
		{
			var chart:ChartBase;
			var tmpSeries:Series;
			if(chartType == "BarChart")
			{
				chart = new BarChart();
				(chart as BarChart).type = type;
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					tmpSeries = new BarSeries();
					(tmpSeries as BarSeries).xField = series[i]['FIELDNAME'];
					tmpSeries.displayName = series[i]['SCRTEXT_L'];
					mySeries.push(tmpSeries);
				}
				
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as BarChart).verticalAxis = myAxis;
				
				outputView.removeAllElements();
				outputView.addElement(chart);
			}
			
			else if(chartType == "ColumnChart")
			{
				chart = new ColumnChart();
				(chart as ColumnChart).type = type;
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					tmpSeries = new ColumnSeries();
					(tmpSeries as ColumnSeries).yField = series[i]['FIELDNAME'];
					tmpSeries.displayName = series[i]['SCRTEXT_L'];
					mySeries.push(tmpSeries);
				}
				
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as ColumnChart).horizontalAxis = myAxis;
				
				outputView.removeAllElements();
				outputView.addElement(chart);
			}
			
			else if(chartType == "AreaChart")
			{
				chart = new AreaChart();
				(chart as AreaChart).type = type;
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					tmpSeries = new AreaSeries();
					(tmpSeries as AreaSeries).yField = series[i]['FIELDNAME'];
					tmpSeries.displayName = series[i]['SCRTEXT_L'];
					mySeries.push(tmpSeries);
				}
				
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as AreaChart).horizontalAxis = myAxis;
				
				outputView.removeAllElements();
				outputView.addElement(chart);
			}
			
			else if(chartType == "LineChart")
			{
				chart = new LineChart();
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					tmpSeries = new LineSeries();
					(tmpSeries as LineSeries).yField = series[i]['FIELDNAME'];
					(tmpSeries as LineSeries).setStyle("form", form);
					tmpSeries.displayName = series[i]['SCRTEXT_L'];
					mySeries.push(tmpSeries);
				}
				
			
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as LineChart).horizontalAxis = myAxis;
				
				outputView.removeAllElements();
				outputView.addElement(chart);
			}
			
			else if(chartType == "PieChart")
			{
				chart = new PieChart();
				
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					tmpSeries = new PieSeries();
					(tmpSeries as PieSeries).field = series[i]['FIELDNAME'];
					tmpSeries.displayName = series[i]['SCRTEXT_L'];
					(tmpSeries as PieSeries).setStyle("labelPosition", form);
					mySeries.push(tmpSeries);
				}
				
				/*var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				*/
				chart.showDataTips = showDataTips;
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
			//	(chart as PieChart).horizontalAxis = myAxis;
				
				outputView.removeAllElements();
				outputView.addElement(chart);
			}
		}
		
		public function chartType_changeHandler(value:String):void
		{
			this.chartType = value;
			
		}
	}
}